package allTests;

import abbot.finder.matchers.NameMatcher;
import abbot.util.AWTFixtureHelper;
import com.supermap.data.Point2D;
import com.supermap.desktop.core.Application;
import operation.*;
import org.junit.*;
import org.junit.rules.ErrorCollector;
import socketEnv.DataUtil;
import util.Info;
import util.ScreenImage;

import javax.swing.*;

import static operation.Operation.robot;
import static operation.RibbonOperation.RIBBONOPERATION;

/**
 * Created by caolp on 2017-04-01.
 */
public class TestVectorAnalyst {
    String picturePath = DataUtil.resultPicturePath + "TestVectorAnalyst\\";
    @Rule
    public final ErrorCollector errorCollector = new ErrorCollector();
    CompareOperation compareOperation = CompareOperation.getInstance(errorCollector);

    @Before
    public void runBeforeTestMethod() {
        Info.out("@Before - runBeforeTestMethod");
        Operation.helper = new AWTFixtureHelper();
        Operation.robot = Operation.helper.getRobot();
        DialogOperation.closeAllDialog();
        Operation.datasources.clear();
    }

    @After
    public void runAfterTestMethod() throws Exception {
        Info.out("@After - runAfterTestMethod");
        Operation.releaseDatasources();
        DialogOperation.closeAllDialog();
        WorkspaceTreeOperation.closeWorkspace();
    }

    @Test
    public void testBufferAnalyst_Point() throws Exception {
        String testDataPath = "VectorAnalyst/BufferAnalyst.udb";
        DataUtil.copyUDB(testDataPath);
        String udbPath = DataUtil.localDataPath + testDataPath;
        String dataSourceType = "打开文件型数据源...";
        new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
        new JFileChooserOperation().chooseFile(udbPath);
        robot.delay(2000);
        CommonOperation commonOperation = new CommonOperation();
        RIBBONOPERATION.clickButton("空间分析", "缓冲区");
        robot.delay(1000);
        DialogOperation dialogOperation = new DialogOperation("生成缓冲区");
        dialogOperation.clickComponent(new NameMatcher("BufferDialog_radioButtonPointOrRegion"));
        dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferData_comboBoxBufferDataDataset"),"Town_P");
        dialogOperation.editTextField(new NameMatcher("PanelResultData_textFieldResultDataDataset"),"result_point");
        dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferRadius_comboBoxUnit"),"千米");
        dialogOperation.editComboBox(new NameMatcher("PanelBufferRadius_numericFieldComboBox"),"3");
        dialogOperation.clickComponent(new NameMatcher("PanelButton_buttonOk"));
        commonOperation.waitForProgressDispose(8000);
        if (Application.getActiveApplication().getActiveForm() == null ){
            Assert.assertTrue("缓冲区分析结果没有打开到地图窗口！",false);
        }
        boolean compareResult = compareOperation.compareDataset("BufferAnalyst","exp_point","result_point");
        Assert.assertTrue("数据集不相等！",compareResult);
        commonOperation.deleteDatasets("BufferAnalyst","result_point");
    }

    @Test
    public void testBufferAnalyst_Line() throws Exception {
        String testDataPath = "VectorAnalyst/BufferAnalyst.udb";
        DataUtil.copyUDB(testDataPath);
        String udbPath = DataUtil.localDataPath + testDataPath;
        String dataSourceType = "打开文件型数据源...";
        new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
        new JFileChooserOperation().chooseFile(udbPath);
        robot.delay(2000);
        RIBBONOPERATION.clickButton("空间分析", "缓冲区");
        robot.delay(1000);
        DialogOperation dialogOperation = new DialogOperation("生成缓冲区");
        dialogOperation.clickComponent(new NameMatcher("BufferDialog_radioButtonLine"));
        dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferData_comboBoxBufferDataDataset"),"River_L");
	    dialogOperation.clickComponent(new NameMatcher("String_UnionBufferItem"));
	    dialogOperation.clickComponent(new NameMatcher("String_DisplayInMap"));
	    dialogOperation.setComboBoxSelectedItem(new NameMatcher("comboBoxUnit"), "千米");
	    dialogOperation.editComboBox(new NameMatcher("numericFieldComboBoxLeft"), "2");
	    dialogOperation.editTextField(new NameMatcher("PanelResultData_textFieldResultDataDataset"),"result_line");
        dialogOperation.clickComponent(new NameMatcher("PanelButton_buttonOk"));
        CommonOperation commonOperation = new CommonOperation();
        commonOperation.waitForProgressDispose(80000);
        robot.delay(2000);
        boolean compareResult = compareOperation.compareDataset("BufferAnalyst","exp_line","result_line");
        Assert.assertTrue("数据集不相等！",compareResult);
        commonOperation.deleteDatasets("BufferAnalyst","result_line");
    }

    @Test
    public void testBufferAnalyst_Region() throws Exception {
        String testDataPath = "VectorAnalyst/BufferAnalyst.udb";
        DataUtil.copyUDB(testDataPath);
        String udbPath = DataUtil.localDataPath + testDataPath;
        String dataSourceType = "打开文件型数据源...";
        new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
        new JFileChooserOperation().chooseFile(udbPath);
        robot.delay(2000);
        RIBBONOPERATION.clickButton("空间分析", "缓冲区");
        robot.delay(1000);
        DialogOperation dialogOperation = new DialogOperation("生成缓冲区");
	    dialogOperation.clickComponent(new NameMatcher("BufferDialog_radioButtonPointOrRegion"));
	    dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferData_comboBoxBufferDataDataset"), "Vegetable_R");
	    dialogOperation.editTextField(new NameMatcher("PanelResultData_textFieldResultDataDataset"),"result_region");
        dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferRadius_comboBoxUnit"),"米");
        dialogOperation.editComboBox(new NameMatcher("PanelBufferRadius_numericFieldComboBox"),"30");
        dialogOperation.clickComponent(new NameMatcher("PanelButton_buttonOk"));
        CommonOperation commonOperation = new CommonOperation();
        commonOperation.waitForProgressDispose(20000);
        new WorkspaceTreeOperation().selectDatasetNodeMenu("BufferAnalyst","Vegetable_R","添加到当前地图");
        robot.delay(4000);
        ScreenImage.printComponentImage((JComponent)new MapOperation().container,picturePath + "testBufferAnalyst_Region.png");
        boolean compareResult = compareOperation.compareDataset("BufferAnalyst","exp_region","result_region");
        Assert.assertTrue("数据集不相等！",compareResult);
        commonOperation.deleteDatasets("BufferAnalyst","result_region");
    }

    @Test
    public void testBufferAnalyst_SelectObject() throws Exception {
        String testDataPath = "VectorAnalyst/BufferAnalyst.udb";
        DataUtil.copyUDB(testDataPath);
        String udbPath = DataUtil.localDataPath + testDataPath;
        String dataSourceType = "打开文件型数据源...";
        new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
        new JFileChooserOperation().chooseFile(udbPath);
        robot.delay(2000);
        new WorkspaceTreeOperation().selectDatasetNodeMenu("BufferAnalyst","Town_P","添加到新地图");
	    robot.delay(2000);
	    Point2D[] point2Ds = {new Point2D(115.87493333333333, 40.22775),new Point2D(116.78933611111111, 39.55439444444444)};
        MapOperation mapOperation = new MapOperation();
        mapOperation.selectObjects(point2Ds);
        CommonOperation commonOperation = new CommonOperation();
        RIBBONOPERATION.clickButton("空间分析", "缓冲区");
        robot.delay(1000);
        DialogOperation dialogOperation = new DialogOperation("生成缓冲区");
        dialogOperation.editTextField(new NameMatcher("PanelResultData_textFieldResultDataDataset"),"result_Objects");
        dialogOperation.setComboBoxSelectedItem(new NameMatcher("PanelBufferRadius_comboBoxUnit"),"千米");
        dialogOperation.editComboBox(new NameMatcher("PanelBufferRadius_numericFieldComboBox"),"4");
        dialogOperation.clickComponent(new NameMatcher("PanelButton_buttonOk"));
        commonOperation.waitForProgressDispose(8000);
        Point2D point2D = new Point2D(116.32820555555556,39.90548333333333);
        RIBBONOPERATION.clickButton("地图", "放大");
        mapOperation.clickPoint(point2D);
        ScreenImage.printComponentImage((JComponent)mapOperation.container,picturePath+"testBufferAnalyst_SelectObject.png");
        boolean compareResult = compareOperation.compareDataset("BufferAnalyst","exp_Objects","result_Objects");
        Assert.assertTrue("数据集不相等！",compareResult);
        commonOperation.deleteDatasets("BufferAnalyst","result_Objects");
    }
}
